package com.Controller.Admin;

import com.Common.ResultModel;
import com.Model.Admin.Role;
import com.Service.Admin.AuthourService;
import com.Service.Admin.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
public class PermissionController {

    @Autowired
    RoleService roleService;
    @Autowired
    AuthourService authourService;

    @RequestMapping(value = "/perm/index")
    public String index() {
        return "admin/permission";
    }

    @RequestMapping(value = "/perm/role")
    @ResponseBody
    public ResultModel getRole() {
        ResultModel rt = new ResultModel();
        List<Role> lst = roleService.getRoleInfos();
        if (CollectionUtils.isEmpty(lst)) {
            rt.setCode(-1);
            rt.setMsg("没有获取到数据");
        } else {
            rt.setCode(0);
            rt.setData(lst);
        }
        return rt;
    }

    /**
     * roleid 为空时获取所有权限信息，不为空时，获取改角色拥有的权限
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/perm/infos")
    @ResponseBody
    public ResultModel getPermission(String roleId) {
        ResultModel rt = new ResultModel();
        List<HashMap<String, Object>> lst = new ArrayList<>();
        if (StringUtils.isEmpty(roleId)) {
            lst = authourService.getAllPerm();
        }
        else {
            lst = authourService.getPermByRole(roleId);
        }
        if (CollectionUtils.isEmpty(lst)) {
            rt.setCode(-1);
            rt.setMsg("没有获取到数据");
        } else {
            rt.setCode(0);
            rt.setData(lst);
        }
        return rt;
    }

}
